/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 获取视频第一帧
 *  */
export const getVideoBase64 = url => {
  return new Promise((resolve, reject) => {
    let dataURL = "";
    const video = document.createElement("video");
    video.setAttribute("crossOrigin", "anonymous"); // 处理跨域
    video.setAttribute("src", url);
    video.setAttribute("width", 260);
    video.setAttribute("height", 140);
    video.setAttribute("autoplay", true);
    video.addEventListener("loadeddata", () => {
      const canvas = document.createElement("canvas");
      const { width } = video; // canvas的尺寸和图片一样
      const { height } = video;
      canvas.width = width;
      canvas.height = height;
      setTimeout(() => {
        canvas.getContext("2d").drawImage(video, 0, 0, width, height); // 绘制canvas
        dataURL = canvas.toDataURL("image/jpeg"); // 转换为base64
        resolve(dataURL);
      }, 1000);
    });
  });
};
